package com.like.hospitalworld;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.GetUserBaseResp;
import com.like.entity.GetUserInfoReq;
import com.like.entity.GetUserInfoResp;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.RetrofitUtil;
import com.like.network.UserService;
import com.like.utils.Constants;
import com.like.utils.ImageUtils;
import com.makeramen.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_personal_info)
public class PersonalInfoActivity extends BaseActivity {

    public static final int REQUEST_REFRESH_USER = 100;

    @ViewInject(R.id.user_icon)
    private ImageView mUserIcon;
    @ViewInject(R.id.username)
    private TextView mUsername;
    @ViewInject(R.id.mp)
    private TextView mMp;
    @ViewInject(R.id.wx_num)
    private TextView mWxNum;

    private GetUserInfoResp mUserInfo;
    private GetUserBaseResp mUserBase;
    private UserService mUserService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserService = RetrofitUtil.getService(UserService.class);
        getUserData();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onGetEvent(EventWrapper event) {
        if (EventWrapper.isMatch(event, getClass(), REQUEST_REFRESH_USER)) {
            getUserData();
        }
    }

    private void getUserData() {
        getUserInfo();
        getUserBase();
    }

    private void getUserInfo() {
        GetUserInfoReq req = new GetUserInfoReq(m.getLoginResp().userID);
        Call<GeneralResponse<GetUserInfoResp>> call = mUserService.getUserInfo(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<GetUserInfoResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<GetUserInfoResp>> call, Response<GeneralResponse<GetUserInfoResp>> response) {
                if (!response.isSuccessful())
                    return;
                mUserInfo = response.body().data;
                setupView(mUserInfo);
            }

            @Override
            public void onFailure(Call<GeneralResponse<GetUserInfoResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void getUserBase() {
        GetUserInfoReq req = new GetUserInfoReq(m.getLoginResp().userID);
        Call<GeneralResponse<GetUserBaseResp>> call = mUserService.getUserBase(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<GetUserBaseResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<GetUserBaseResp>> call, Response<GeneralResponse<GetUserBaseResp>> response) {
                if (!response.isSuccessful())
                    return;
                mUserBase = response.body().data;
                setupView(mUserBase);
            }

            @Override
            public void onFailure(Call<GeneralResponse<GetUserBaseResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void setupView(GetUserInfoResp userInfo) {
        ImageUtils.showImage(m.mContext, userInfo.head, m.getToken(), mUserIcon);
        mUsername.setText(userInfo.nickName.trim());
    }

    private void setupView(GetUserBaseResp userBase) {
        mMp.setText(userBase.mobile);
        mWxNum.setText(userBase.openId);
    }

    @Event(R.id.edit_pwd_container)
    private void editPwdClick(View v) {
        Intent intent = new Intent(m.mContext, EditPwdActivity.class);
        startActivity(intent);
    }

    @Event(R.id.edit_username_container)
    private void editUsernameClick(View v) {
        Intent intent = new Intent(m.mContext, EditUsernameActivity.class);
        intent.putExtra("nickname", mUserInfo.nickName);
        startActivity(intent);
    }

    @Event(R.id.mp_container)
    private void editMpClick(View v) {
        Intent intent = new Intent(m.mContext, EditMpActivity.class);
        startActivity(intent);
    }

    @Event(R.id.real_name_auth_container)
    private void realNameAuthClick(View v) {
        Intent intent = new Intent(m.mContext, RealNameAuthActivity.class);
        startActivity(intent);
    }

}
